<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加车辆</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layui/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form" style="padding: 20px" method="post" enctype="multipart/form-data" id="carForm">
    <input type="hidden" name="carid" id="carid">
    <input type="hidden" id="btype">
    <div class="layui-form-item">
        <label class="layui-form-label">车牌号:</label>
        <div class="layui-input-block">
            <input type="text" id="carplate" name="carplate" carplate  lay-verify="required" placeholder="请输入车牌号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">品牌:</label>
        <div class="layui-input-block">
            <input type="text" id="carbrand" name="carbrand" required lay-verify="required" placeholder="请输入品牌" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">类型:</label>
        <div class="layui-input-block">
            <input type="text" id="cartype" name="cartype" required  lay-verify="required" placeholder="请输入车辆类型" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">颜色:</label>
        <div class="layui-input-block">
            <input type="text" id="carcolor" name="carcolor" required  lay-verify="required" placeholder="请输入车辆颜色" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">座位数:</label>
        <div class="layui-input-block">
            <input type="text" id="carseat" name="carseat" required  lay-verify="required" placeholder="请输入车辆座位数" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">发动机型号:</label>
        <div class="layui-input-block">
            <input type="text" id="engineno" name="engineno" required  lay-verify="required" placeholder="请输入发动机型号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">L/100km:</label>
        <div class="layui-input-block">
            <input type="text" id="car_consumption" name="car_consumption" required  lay-verify="required" placeholder="请输入百公里耗油量" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">购买价格:</label>
        <div class="layui-input-block">
            <input type="text" id="price" name="price" required  lay-verify="required" placeholder="请输入购买价格" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">购买时间:</label>
        <div class="layui-input-block">
            <input type="text" id="buy_date" name="buy_date" required  lay-verify="required" placeholder="请输入购买时间" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">保养里程:</label>
        <div class="layui-input-block">
            <input type="text" id="car_servicekm" name="car_servicekm" required  lay-verify="required" placeholder="请输入保养里程" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">供应商:</label>
        <div class="layui-input-block">
            <select id="btypeid" lay-verify="required" name="btype.btypeid">
                <option value="请选择供应商">请选择供应商</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">所属部门:</label>
        <div class="layui-input-block">
            <select id="deptid" lay-verify="required" name="dept.deptid">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">原有司机:</label>
        <div class="layui-input-block">
            <input type="text" id="driverOld"  required  lay-verify="required" autocomplete="off" class="layui-input" disabled>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">分配司机:</label>
        <div class="layui-input-block">
            <select id="driverid" lay-verify="required" name="driver.driverid">
                <option value="请选择司机">请选择司机</option>
            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">图片:</label>
        <button type="button" class="layui-btn" id="img_url">
            <i class="layui-icon">&#xe67c;</i>上传图片
        </button>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <img id="carImage"   width="150px" height="150px" src="/static/layui/images/wu.png">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">备注:</label>
        <div class="layui-input-block">
            <input type="text" id="remark" name="remark" required  lay-verify="required" placeholder="无" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="carEdit" id="addBtn">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/layui/layui/layui.js"></script>
<script>
    layui.use(['form','layer','laydate','table','laytpl','upload'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table,
            upload=layui.upload;

        //绑定时间插件到生日文本框中
        laydate.render({
            elem:'#buy_date',
            theme: 'grid'
        });

        form.render("select");

        //文件上传
        upload.render({
            elem:'#img_url',
            url: "/car/editCar",
            auto:false,
            before:function (obj) {
                this.data={
                    carid:$("#carid").val(),
                    carplate:$("#carplate").val(),
                    carbrand:$("#carbrand").val(),
                    cartype:$("#cartype").val(),
                    carcolor:$("#carcolor").val(),
                    carseat:$("#carseat").val(),
                    engineno:$('#engineno').val(),

                    car_consumption:$("#car_consumption").val(),
                    price:$("#price").val(),
                    buy_date:$("#buy_date").val(),
                    car_servicekm:$("#car_servicekm").val(),
                    deptid:$("#deptid").val(),
                    btypeid:$('#btypeid').val(),
                    driverid:$("#driverid").val(),
                    remark:$('#remark').val()

                };
                console.log(this.data);
            },
            choose:function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#carImage').attr('src', result); //图片链接（base64）
                });
            }
        });
        //获取空闲司机
        $.ajax({
            url:"/driver/noCarDriver",
            type:"get",
            success:function (res) {

                for(var i=0;i<res.length;i++){
                    var op=$('<option></option>');
                    op.attr('value',res[i].driverid);
                    op.append(res[i].dirvername);
                    $("#driverid").append(op);
                }
                form.render("select");

            }
        });
        //获取往来单位
        $.ajax({
            url:"/btype/carBtype",
            type:"get",
            success:function (res) {
                for(var i=0;i<res.length;i++){
                    var op=$('<option></option>')
                    op.attr('value',res[i].btypeid);
                    if($("#btype").val()==res[i].btypeid){
                        op.attr("selected","selected");
                    }
                    op.append(res[i].btypename);
                    $('#btypeid').append(op);
                }
                form.render("select");
            }
        });
        //提交表单
        form.on("submit(carEdit)",function(data) {
            var formData = new FormData($("#carForm")[0]);
            $.ajax({
                cache: true,
                type: "post",
                url: "/car/editCar",
                async: false,
                data: formData,  // 你的formid
                contentType: false,   //jax 中 contentType 设置为 false 是为了避免 JQuery 对其操作，从而失去分界符，而使服务器不能正常解析文件
                processData: false,   //当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
                success: function (res) {
                    console.log("进不去回调")
                    console.log(res);
                    if(res.code==200){
                        layui.layer.msg(res.msg);
                        //关闭窗口ifram
                        var index1=parent.layer.getFrameIndex(window.name)
                        setTimeout(function () {
                            parent.layer.close(index1);
                            window.parent.location.reload();
                        },1000)

                    }else if(res.code==-200){
                        layui.layer.msg(res.msg);
                        //关闭窗口ifram
                        var index1=parent.layer.getFrameIndex(window.name)
                        setTimeout(function () {
                            parent.layer.close(index1);
                        },1000)
                    }
                }
            })
            return false;
        });


    })
</script>
</body>
</html>